package cn.shyouchi.wuguanjia.android.ui.patrol.begin;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.patrol.PatrolApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.SelManagerEvent;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PatrolSelManagerActivity extends BaseActivity {


    PatrolApi mPatrolApi;
    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_leader)
    RecyclerView mRvLeader;
    @BindView(R.id.btn_save)
    Button mBtnSave;

    PatrolSelManagerAdapter mSelManagerAdapter;
    List<PatrolManagerEntity.ResultMessageBean> managers = new ArrayList<>();


    @Override
    public int initContentView() {
        return R.layout.activity_patrol_sel_manager;
    }

    @Override
    public void initInjector() {
        mPatrolApi = new PatrolApi(mContext);

    }

    @Override
    public void initUiAndListener() {

        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        mSelManagerAdapter = new PatrolSelManagerAdapter(managers);
        mRvLeader.setAdapter(mSelManagerAdapter);
        mRvLeader.setLayoutManager(new LinearLayoutManager(mContext));

        mPatrolApi.getManagers()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<PatrolManagerEntity>(mContext) {
                    @Override
                    public void onNext(PatrolManagerEntity entity) {
                        managers.addAll(entity.getResultMessage());
                        mSelManagerAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }



    @OnClick(R.id.btn_save)
    public void onClick() {

        EventBus.getDefault().post(new SelManagerEvent(mSelManagerAdapter.getSelData()));
        finish();

    }
}
